package com.snoob.lixing.controller;

import com.github.pagehelper.PageInfo;
import com.snoob.lixing.pojo.Classes;
import com.snoob.lixing.service.ClassesService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * User: lixing
 * Date: 2018/11/24 20:37
 */
@Controller
@RequestMapping("classes")
public class ClassesController {

    @Resource
    private ClassesService classesService;

    /**
     * 分页查询
     *
     * @param classes    分页条件
     * @param pageNumber 分页页码
     * @param pageSize   分页大小
     * @return
     * @throws Exception
     */
    @GetMapping("page/{pageNumber}/{pageSize}")
    public ModelAndView page(Classes classes, @PathVariable Integer pageNumber, @PathVariable Integer pageSize) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("classes/classesList");

        Classes pojo = new Classes();
        pojo.setName("张三");
        pojo.setClassesNum("310112");
        pojo.setCreateTime(new Date());
        pojo.setCreateTimeStart(new Date());
        pojo.setCreateTimeEnd(new Date());
        PageInfo page = classesService.page(pojo, pageNumber, pageSize);
        List<Classes> classesList = classesService.selectPojo(pojo);

        mv.addObject("page", page);
        mv.addObject("classesList", classesList);
        return mv;
    }

    /**
     * 查询
     *
     * @param classes 查询条件
     * @return
     * @throws Exception
     */
    @PostMapping("select")
    public ModelAndView select(@RequestBody Classes classes) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("classes/classesDetail");
        mv.addObject("classesList", classesService.selectPojo(classes));
        return mv;
    }

    /**
     * 编辑（Ajax异步操作）
     *
     * @param classes id为空为添加、id为空为更新
     * @return
     * @throws Exception
     */
    @PostMapping("modify")
    @ResponseBody
    public String modify(@RequestBody Classes classes) throws Exception {
        return null;
    }

    /**
     * 删除（Ajax异步操作）
     *
     * @param pojoIdStr 班级编号字符串,多个以逗号分隔
     * @return
     * @throws Exception
     */
    @PostMapping("delete")
    @ResponseBody
    public String delete(@RequestParam String pojoIdStr) throws Exception {
        return null;
    }

    /**
     * 导入Excel（Ajax异步操作）
     *
     * @param classesExcelFile Excel文件
     * @param action           导入指令：insert(批量添加)、update(批量更新)、delete(批量删除)
     * @return
     * @throws Exception
     */
    @PostMapping("importExcel/{action}")
    @ResponseBody
    public String importExcel(@RequestPart("classesExcelFile") MultipartFile classesExcelFile, @PathVariable String action) throws Exception {
        return null;
    }

    /**
     * 导出Excel
     *
     * @param response 响应Excel文件到浏览器
     * @param classes  导出条件
     * @throws Exception
     */
    @PostMapping("exportExcel")
    public void exportExcel(@RequestBody Classes classes, HttpServletResponse response) throws Exception {

    }

}
